package red.stu.mc.mod.codeblock.client.gui;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import red.stu.mc.mod.codeblock.CodeBlockMod;
import red.stu.mc.mod.codeblock.tileentity.TileEntityCodeBlock;
import red.stu.window.editor.CodeEditorWindow;
import red.stu.window.editor.code.CodeEditor.Callback;

@SideOnly(Side.CLIENT)
public class GuiContainerCodeBlock extends GuiContainer {

	private static final ResourceLocation BACKAGE = new ResourceLocation(
			CodeBlockMod.MODID + ":" + "textures/gui/container/code_block_background.png");

	private static TileEntityCodeBlock tileentity;
	private static EntityPlayer playerIn;

	private static CodeEditorWindow ideWindow;
	
	private String title = "";

	static {
		ideWindow = new CodeEditorWindow(new Callback() {
			@Override
			public void onEscKeyPressed() {
				ideWindow.setVisible(false);
			}
		});
		ideWindow.addComponentListener(new ComponentListener() {

			@Override
			public void componentShown(ComponentEvent e) {
				ideWindow.refreshCodeEditorText();
			}

			@Override
			public void componentResized(ComponentEvent e) {
				
			}

			@Override
			public void componentMoved(ComponentEvent e) {

			}

			@Override
			public void componentHidden(ComponentEvent e) {
				if (playerIn != null)
					playerIn.closeScreen();
				tileentity.setCode(ideWindow.codeEditor.getText());
				ideWindow.setVisible(false);
			}
		});
	}

	public GuiContainerCodeBlock(Container inventorySlotsIn) {
		super(inventorySlotsIn);
		this.xSize = 248;
		this.ySize = 166;
	}

	public static void initIDEGUI() {

	}

	// 用于传递玩家打开窗口前的一些参数
	public static void onIdeGuiOpen(TileEntityCodeBlock tileentity, EntityPlayer playerIn) {
		GuiContainerCodeBlock.tileentity = tileentity;
		GuiContainerCodeBlock.playerIn = playerIn;
	}

	@Override
	public void initGui() {
		/*
		 * ideWindow.setBounds(Display.getX(), Display.getY(), Display.getWidth(),
		 * Display.getHeight());
		 */
		String code = tileentity.getCode();
		ideWindow.codeEditor.setText(code);
		BlockPos pos = tileentity.getPos();
		title = tileentity.getGroup() + "   " + pos.getX() + " / " + pos.getY() + " / " + pos.getZ();
		ideWindow.setTitle(title);
		ideWindow.requestFocusInWindow();
		ideWindow.requestFocus();
		ideWindow.setVisible(true);
		super.initGui();
		for (int i = 0; i < 3; i++) {
			try {
				Thread.sleep(220);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			ideWindow.setVisible(true);
		}
	}

	@Override
	public void onGuiClosed() {
		tileentity.setCode(ideWindow.codeEditor.getText());
		ideWindow.setVisible(false);
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GlStateManager.color(1.0F, 1.0F, 1.0F);
		this.mc.getTextureManager().bindTexture(BACKAGE);
		int offsetX = (this.width - this.xSize) / 2, offsetY = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(offsetX, offsetY, 0, 0, this.xSize, this.ySize);
        this.drawString(fontRenderer, title, offsetX + 28, offsetY + 8, 5287);
	}

}
